package com.easy.tool.utils.ajax;

import com.easy.tool.anno.MethodDes;
import com.easy.tool.enums.ResponseEnum;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 高俊
 * @create 2022-05-2022/5/26-15:39
 */
@Data
public class MapResult<T> {
    private Map<String ,Object> map = null;
    /**
     * 请求成功
     * @return
     */
    @MethodDes(des = "请求成功",explain = "请求成功")
    public static MapResult ok(){
        Map<String, Object> map = new HashMap<>(5);
        map.put("code", ResponseEnum.OK.getCode());
        map.put("codeName",ResponseEnum.OK.getCodeName());
        return new MapResult<Void>(map);
    }

    /**
     * 请求成功
     * @param data
     * @param <T>
     * @return
     */
    @MethodDes(des = "请求成功",explain = "请求成功调用方法，添加默认的请求成功code和codeName")
    public static <T> MapResult ok(T data){
        Map<String, Object> map = new HashMap<>(5);
        map.put("code", ResponseEnum.OK.getCode());
        map.put("codeName",ResponseEnum.OK.getCodeName());
        map.put("data",data);
        return new MapResult(map);
    }

    /**
     * 请求失败
     * @param code
     * @param codeName
     * @return
     */
    @MethodDes(des = "请求失败",explain = "请求失败调用方法，自定翼code和codeName",param = {"arg0->code：请求响应码","arg1->codeName：请求响应码名称"})
    public static <T> MapResult faild(Integer code,String codeName){
        Map<String, Object> map = new HashMap<>(5);
        map.put("code", code);
        map.put("codeName",codeName);
        return new MapResult(map);
    }

    /**
     * 添加data数据
     * @param data
     * @return
     */
    @MethodDes(des = "添加data数据",explain = "添加data数据，通过ok或者faild方法调用之后，链式调用",param = {"arg0-> data：返回页面需要携带的数据"})
    public MapResult data(T data){
        Map<String, Object> map = this.getMap();
        map.put("data",data);
        this.setMap(map);
        return this;
    }

    /**
     * 添加msg消息
     * @param msg
     * @return
     */
    @MethodDes(des = "添加msg消息",explain = "添加msg消息",param = {"arg0->msg：返回页面需要自定义添加的消息信息"})
    public MapResult msg(String msg){
        Map<String, Object> map = this.getMap();
        this.getMap().put("msg",msg);
        this.setMap(map);
        return this;
    }

    /*--------------------------------------------------------*/

    /**
     * 自定义返回
     * @return
     */
    @MethodDes(des = "自定义返回",explain = "自定义返回",param = {"arg0->code：请求响应码","arg1->codeName：请求响应码名称"})
    public static MapResult customResult(Integer code,String codeName){
        Map<String, Object> map = new HashMap<>(5);
        map.put("code", ResponseEnum.OK.getCode());
        map.put("codeName",ResponseEnum.OK.getCodeName());
        return new MapResult(map);
    }


    /*--------------------------------------------------------*/

    public MapResult() {
    }

    public MapResult(Map<String, Object> map) {
        this.map = map;
    }

    private Map<String, Object> getMap() {
        return map;
    }

    private void setMap(Map<String, Object> map) {
        this.map = map;
    }
}
